/**
 * ajax操作的一些基础类
 */
function HttpObject() {
	if (document.all) {
		this.httpObj = new ActiveXObject("Microsoft.XMLHttp");
	} else {
		this.httpObj = new XMLHttpRequest();
	}
	
	this.contentType = "application/x-www-form-urlencoded";
	this.isAsync = false;
	this.retXml = true;
	
	this.handleXmlResponse = function (response) {
		var doc = response.documentElement;
		if (typeof this.handleXmlString == "function") {
			var retString = this.parseToString(doc);
			this.handleXmlString(retString);
		}
		
		if (typeof this.handleXmlArray == "function") {
			var retArray = this.parseToArray(doc);
			this.handleXmlArray(retArray);
		}
		
		if (typeof this.handleXmlObject == "function") {
			var retObjects = this.parseToObjects(doc);
			this.handleXmlObject((retObjects.length > 0)?retObjects[0]:null);
		}
		
		if (typeof this.handleXmlObjects == "function") {
			var retObjects = this.parseToObjects(doc);
			this.handleXmlObjects(retObjects)
		}
		
		if (typeof this.handleXmlMaps == "function") {
			var retMaps = this.parseToMaps(doc);
			this.handleXmlMaps(retMaps);
		}
		
		if (typeof this.handleXmlDoc == "function") {
			this.handleXmlDoc(doc);	
		}	
	}
	this.handleTxtResponse = null;
	
	//异步调用可重载的事件.
	this.handleXmlString = null;
	this.handleXmlArray = null;
	this.handleXmlObject = null;
	this.handleXmlObjects = null;
	this.handleXmlMaps = null;
	this.handleXmlDoc = null;
	
	this.getXmlText = function (node) {
		if (node.firstChild && node.firstChild != null && node.firstChild.nodeType == 3)
			return node.firstChild.nodeValue;
		else if (node.firstChild && node.firstChild != null && node.firstChild.nodeType == 4)
			return node.firstChild.nodeValue;
		else
			return null;
	};
}

HttpObject.prototype.httpGetReturnMaps = function (URL) {
	if (this.retXml) {
		var doc = this.httpGet(URL);
		return this.parseToMaps(doc);
	} else 
		return null;
}
HttpObject.prototype.httpPostReturnMaps = function (URL, data) {
	if (this.retXml) {
		var doc = this.httpPost(URL, data);
		return this.parseToMaps(doc);
	} else 
		return null;
}
HttpObject.prototype.parseToMaps = function (doc) {
	var ret = new Array();
	var nds1 = doc.getElementsByTagName('object');
	for(var i = 0; i < nds1.length; i++ ) { 
		var nds2 = nds1.item(i).getElementsByTagName('property');
		ret[i] = {};
		for(var j = 0; j < nds2.length; j++ ) { 
			var p = nds2.item(j);
			var pn = p.getAttribute("name");
			var pv = this.getXmlText(p);
			ret[i][pn] = pv;
		}
	}
	return ret;
}
HttpObject.prototype.httpGetReturnObject = function (URL) {
	var ret = this.httpGetReturnObjects(URL)
	return (ret.length > 0)?ret[0]:null;	
}

HttpObject.prototype.httpPostReturnObject = function (URL, data) {
	var ret = this.httpPostReturnObjects(URL, data)
	return (ret.length > 0)?ret[0]:null;
}

HttpObject.prototype.httpGetReturnObjects = function (URL) {
	if (this.retXml) {
		var doc = this.httpGet(URL);
		return this.parseToObjects(doc);
	} else 
		return null;
}
HttpObject.prototype.httpPostReturnObjects = function (URL, data) {
	if (this.retXml) {
		var doc = this.httpPost(URL, data);
		return this.parseToObjects(doc);
	} else 
		return null;
}
HttpObject.prototype.parseToObjects = function (doc) {
	var ret = new Array();
	var nds1 = doc.getElementsByTagName('object');
	for(var i = 0; i < nds1.length; i++ ) { 
		var nds2 = nds1.item(i).getElementsByTagName('property');
		ret[i] = new Object();
		for(var j = 0; j < nds2.length; j++ ) { 
			var p = nds2.item(j);
			var pn = p.getAttribute("name");
			var pv = this.getXmlText(p);
			eval("ret[i]." + pn + " = pv;");
		}
	}
	return ret;
}
HttpObject.prototype.httpGetReturnString = function (URL) {
	if (this.retXml) {
		var doc = this.httpGet(URL);
		return this.parseToString(doc);
	} else {
		return this.httpGet(URL);
	}
}
HttpObject.prototype.httpPostReturnString = function (URL, data) {
	if (this.retXml) {
		var doc = this.httpPost(URL, data);
		return this.parseToString(doc);
	} else {
		return this.httpPost(URL, data);
	}
}
HttpObject.prototype.parseToString = function (doc) {
	var nodes = doc.getElementsByTagName('value');
	if (nodes.item(0))
		return this.getXmlText(nodes.item(0));
	else
		return null;
}
HttpObject.prototype.httpGetReturnArray = function (URL) {
	if (this.retXml) {
		var doc = this.httpGet(URL);
		return this.parseToArray(doc);
	} else
		return null;
}
HttpObject.prototype.httpPostReturnArray = function (URL, data) {
	if (this.retXml) {
		var doc = this.httpPost(URL, data);
		return this.parseToArray(doc);
	} else
		return null;
}
HttpObject.prototype.parseToArray = function (doc) {
	var ret = new Array();
	var nodes = doc.getElementsByTagName('value');
	for(var i = 0; i < nodes.length; i++ ){ 
		ret[i] = this.getXmlText(nodes.item(i));
	}
	return (ret.length == 0)?null:ret;
}
HttpObject.prototype.httpGet = function (URL) {
	this.httpObj.open("GET", globeWebAppContext + URL, this.isAsync);
	this.httpObj.setRequestHeader("Content-Type", this.contentType);

	if (this.isAsync) {
		var obj = this;
		this.httpObj.onreadystatechange = function () {
			if (obj.httpObj.readyState == 4 && obj.httpObj.status == 200) {
				if (typeof obj.handleXmlResponse == "function") {
					obj.handleXmlResponse(obj.httpObj.responseXML);
				}
				if (typeof obj.handleTxtResponse == "function") {
					obj.handleTxtResponse(obj.httpObj.responseText);
				}
			}
		}
	}
	
	this.httpObj.send("");
	
	if (!this.isAsync) {
		if (this.retXml) {
			var response = this.httpObj.responseXML;
			return (response && response != null)?response.documentElement:null;
		} else {
			var response = this.httpObj.responseText;
			return (response && response != null)?response.trim():null;
		}
	}
}
HttpObject.prototype.httpPost = function (URL, data) {
	this.httpObj.open("POST", globeWebAppContext + URL, this.isAsync);
	this.httpObj.setRequestHeader("Content-Type", this.contentType);
	
	if (this.isAsync) {
		var obj = this;
		this.httpObj.onreadystatechange = function () {
			if (obj.httpObj.readyState == 4 && obj.httpObj.status == 200) {
				if (typeof obj.handleXmlResponse == "function") {
					obj.handleXmlResponse(obj.httpObj.responseXML);
				}
				
				if (typeof obj.handleTxtResponse == "function") {
					obj.handleTxtResponse(obj.httpObj.responseText);
				}
			}
		}
	}
	if (data) {
		this.httpObj.send(data);
	} else {
		this.httpObj.send("");
	}
	
	if (!this.isAsync) {
		if (this.retXml) {
			var response = this.httpObj.responseXML;
			return (response && response != null)?response.documentElement:null;
		} else {
			var response = this.httpObj.responseText;
			return (response && response != null)?response.trim():null;
		}
	}
}
